#include <stdint.h>
#include <string.h>

#include "hdf_device_desc.h" 
#include "hdf_log.h"         
#include "device_resource_if.h"
#include "osal_io.h"
#include "osal_mem.h"
#include "osal_time.h"

#define TOPIC  "\"羌笛何须怨杨柳，春风不度玉门关\"的作者是？"
#define OP_A "王维"
#define OP_B "李白"
#define OP_C "王焕之"
#define OP_D "白居易"
#define ANSWER OP_C

struct topic
{
    struct HdfSBuf *Topic;
    struct HdfSBuf *A;
    struct HdfSBuf *B;
    struct HdfSBuf *C;
    struct HdfSBuf *D;
    struct HdfSBuf *Answer;
};

struct topic g_topic_data;


int32_t TopicDriverDispatch(struct HdfDeviceIoClient *client, int cmdCode, struct HdfSBuf *data, struct HdfSBuf *reply)
{
    char *replay_buf;
    HDF_LOGE("Topic driver dispatch");
    if (client == NULL || client->device == NULL)
    {
        HDF_LOGE("Topic driver device is NULL");
        return HDF_ERR_INVALID_OBJECT;
    }
    switch (cmdCode)
    {
    case 1:
        HdfSbufWriteString(g_topic_data.Topic,TOPIC);
        HdfSbufWriteString(g_topic_data.A,OP_A);
        HdfSbufWriteString(g_topic_data.B,OP_B);
        HdfSbufWriteString(g_topic_data.C,OP_C);
        HdfSbufWriteString(g_topic_data.D,OP_D);
        HdfSbufWriteString(g_topic_data.Answer,ANSWER);
        replay_buf =  OsalMemAlloc(600);
        (void)memset_s(replay_buf, 600, 0, 600);
        sprintf(replay_buf,"{\"topic\":%s,\"A\":%s,\"B\":%s,\"C\":%s,\"D\":\"%s\",\"answer\":%s}",\
                            g_topic_data.Topic,           \
                            g_topic_data.A,      \
                            g_topic_data.B,   \
                            g_topic_data.C, \
                            g_topic_data.D, \
                            g_topic_data.Answer);

        if (!HdfSbufWriteString(reply, replay_buf))                
        {
            HDF_LOGE("replay is fail");
            return HDF_FAILURE;
        }
        OsalMemFree(replay_buf);
        break;
    default:
        return HDF_FAILURE;
    }
    return HDF_SUCCESS;
}

static int32_t Hdf_Topic_DriverBind(struct HdfDeviceObject *deviceObject)
{
    if (deviceObject == NULL)
    {
        HDF_LOGE("topic driver bind failed!");
        return HDF_ERR_INVALID_OBJECT;
    }
    static struct IDeviceIoService topicDriver = { 
        .Dispatch = TopicDriverDispatch,
    };
    deviceObject->service = (struct IDeviceIoService *)(&topicDriver);
    HDF_LOGD("Topic driver bind success");
    return HDF_SUCCESS;
}

static int32_t Hdf_Topic_DriverInit(struct HdfDeviceObject *device)
{
    return HDF_SUCCESS;
}

void Hdf_Topic_DriverRelease(struct HdfDeviceObject *deviceObject)
{
    if (deviceObject == NULL)
    {
        HDF_LOGE("Topic driver release failed!");
        return;
    }
    HDF_LOGD("Topic driver release success");
    return;
}


struct HdfDriverEntry g_topic_driver_entry = {
    .moduleVersion = 1,
    .moduleName = "HDF_TOPIC",
    .Bind = Hdf_Topic_DriverBind,
    .Init = Hdf_Topic_DriverInit,
    .Release = Hdf_Topic_DriverRelease,
};

//将驱动入口注册到HDF框架中
HDF_INIT(g_topic_driver_entry);